<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import serachUserid from '@/components/serachUserid/index.vue';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    formType:1
  })
  interface formQuert {
    rate:string
    user_id:number
  }
  let form = reactive({} as formQuert)
  const validateLength = (rule: any, value: any, callback: any)=>{
    if(!value) {
      callback(new Error('请输入税率(百分位)'))
    }else if(value > 10 || value < 0) {
      callback(new Error('请输入0-10之间数字'))
    } else {
      callback()
    }
  }
  const formRules = {
    rate: [{ required: true, trigger: 'blur', validator:validateLength,}],
    user_id: [{ required: true, trigger: 'blur', message: '请输入用户UUID' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { formType } = action
      const loading = Toastloading()
      let submitType = false
      if(formType == 1) {
        await numberApi.agentGolodUserAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitType = true
        })
      }
      if(formType == 2) {
        await numberApi.agentGolodUserEdit(form).then(()=>{
          ToastSuccess('修改成功')
          submitType = true
        })
      }
      if(submitType) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const {
        nickname='',
        user_id=0,
        rate=0
    } = query
    let formType = 0
    if(!user_id) {
      action.title = '新增代理用户'
      formType = 1
    }
    if(user_id) {
      formType = 2
      Object.assign(form,{
        user_id,
        rate
      })
      action.title = `修改代理用户:${nickname}`
    }
    action.formType = formType
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="10vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item v-if="action.formType == 1" :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="税率(百分位)" prop="rate">
          <el-input
            v-model="form.rate"
            type="number"
            placeholder="请输入0-10之间数字"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
